package com.eibus.applicationconnector.dotnet;

import junit.framework.TestCase;
import com.eibus.connector.nom.Connector;
import com.eibus.xml.nom.Node;
import com.eibus.xml.xpath.XPath;

//@Author	: Lejil
//@Class	: DotnetDifferentTypesTest

/**
 *	<br/>Owner:			Lejil
 *	<br/>Description:	Invoke the .NET public Instance & Static Methods,for default values, different combinations of types etc..
 */
public class DotnetDifferentTypesErrorTestCase extends TestCase
{
	Connector  connector;
	private final String namespace 		="http://schemas.cordys.com/1.0/dotnet/RefUnitTest";
	private final String unitNamespace	="http://schemas.cordys.com/1.0/dotnet/UnitTestMethods";
	private final String faultCode		="Arguments.Type-Mismatch";

	protected void setUp()
	{
		try
		{
			connector = Connector.getInstance("DotNET Client");
			if (!connector.isOpen())
				connector.open();

		}
		catch(Exception exception)
		{
			fail("Could not get instance of Connector. "+exception.getMessage());
		}
	}

	protected void tearDown()
	{

	}

	public DotnetDifferentTypesErrorTestCase(String name)
	{
		super(name);
	}

	public static void main(String[] args)
	{
		junit.textui.TestRunner.run(DotnetDifferentTypesErrorTestCase.class);
	}

	public  boolean checkforSoapFault(int response) throws Exception
	{
		
		try
		{
			int  faultNode = XPathUtil.getSOAPFault(response);

			if ( faultNode == 0)
			{
				fail(".NET Connector Expected soapFault, but returned Incorrect result" );
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}


	public void testMixedTypeMethodsWithInt() //1
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"intMixedArgs");
			Node.createTextElement("intArgs","10o0",request);
			Node.createTextElement("longArgs","3456012",request);
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<SOAP:faultcode>","CorrectResult");
            String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals(faultCode))
			{
				//fail("testByteType Expected value is "+ faultCode+", but returned " + result);
			}

		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- intMixedArgs :: " + ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
		}
	}

	public void testMixedTypeStaticMethodsWithInt() //2
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"intStaticMixedArgs");
			Node.createTextElement("intArgs","10a0",request);
			Node.createTextElement("longArgs","3456012",request);
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals(faultCode))
			{
				//fail("testMixedTypeStaticMethodsWithInt Expected value is "+ faultCode+", but retrunred " + result);
			}

		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- intMixedArgs :: " +ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));

		}

	}


	public void testMixedTypeRefMethodsWithInt() //3
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"intRefMixedArgs");
			Node.createTextElement("intArgs","1a00",request);
			Node.createTextElement("longArgs","3456012",request);
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals(faultCode))
			{
				//fail("testMixedTypeRefMethodsWithInt Expected value is "+ faultCode+", but retrunred " + result);
			}

		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : intRefMixedArgs :: " +ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));

		}

	}
	public void testMixedTypeRefStaticMethodsWithint() //4
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"intStaticRefMixedArgs");
			Node.createTextElement("intArgs","a",request);
			Node.createTextElement("longArgs","3456012",request);
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals(faultCode))
			{
				//fail("testMixedTypeRefStaticMethodsWithint Expected value is "+ faultCode+", but retrunred " + result);
			}

		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : intStaticRefMixedArgs :: " +ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));

		}

	}


	public void testMixedTypeMethodsWithString() //1
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"stringMixedArgs");
			Node.createTextElement("string","Cordys Inida Pvt",request);
			Node.createTextElement("xml","<ofsfice>Cordys</office>",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals("Server.NotImplemented"))
			{
				//fail("testMixedTypeMethodsWithString Expected value is "+ faultCode+", but retrunred " + result);
			}

		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- stringMixedArgs :: " +ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));

		}
	}

	public void testMixedTypeStaticMethodsWithString() //2
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"stringStaticMixedArgs");
			Node.createTextElement("string","Cordys Inida Pvt",request);
			Node.createTextElement("xml","<offisce>Cordys</office>",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals("Server.NotImplemented"))
			{
				//fail("testMixedTypeStaticMethodsWithString Expected value is "+ faultCode+", but retrunred " + result);
			}

		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- stringStaticMixedArgs :: "+ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));

		}

	}


	public void testMixedTypeRefMethodsWithString() //3
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"stringRefMixedArgs");
			Node.createTextElement("string","Cordys",request);
			Node.createTextElement("xml","<offisce>Cordys India</office>",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals("Server.NotImplemented"))
			{
				//fail("testMixedTypeRefMethodsWithString Expected value is "+ faultCode+", but retrunred " + result);
			}

		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : stringRefMixedArgs :: "+ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));

		}

	}
	public void testMixedTypeRefStaticMethodsWithString() //4
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"stringStaticRefMixedArgs");
			Node.createTextElement("string","Cordys",request);
			Node.createTextElement("xml","<officse>Cordys India</office>",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals("Server.NotImplemented"))
			{
				//fail("testMixedTypeRefStaticMethodsWithString Expected value is "+ faultCode+", but retrunred " + result);
			}

		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : stringStaticRefMixedArgs :: "+ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));

		}

	}



	public void testInvalidAssembly()
	{
		int response =0,request =0;
		try
		{
			request 	= connector.createSOAPMethod(unitNamespace,"testInvalidAssembly");
			Node.createTextElement("intArgs1","343545",request);
			Node.createTextElement("intArgs2","2444043",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals("Assembly.NotFound"))
			{
				//fail("testInvalidAssembly Expected value is Assembly.NotFound, but retrunred " + result);
			}
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- testInvalidAssembly");
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			Util.delete(Node.getRoot(request));
		}

	}
	public void testInvalidClass()
	{
		int response =0,request =0;
		try
		{
			request 	= connector.createSOAPMethod(unitNamespace,"testInvalidClass");
			Node.createTextElement("IntArgs1","22324",request);
			Node.createTextElement("IntArgs2","244403",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals("Server.NotImplemented"))
			{
				//fail("testInvalidClass Expected value is Server.NotImplemented, but retrunred " + result);
			}
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- TestDoubleType :: "+ex.toString());
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			Util.delete(Node.getRoot(request));
		}
	}

	public void testInvlaidMethod()
	{
		int response =0,request =0;
		try
		{
			request 	= connector.createSOAPMethod(unitNamespace,"testInvlaidMethod");
			Node.createTextElement("lngArgs1","22324313",request);
			Node.createTextElement("lngArgs2","24440333",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals("Method.Not-Found"))
			{
				//fail("testInvlaidMethod Expected value is Method.Not-Found, but retrunred " + result);
			}
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- testInvlaidMethod :: "+ex.toString());
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			Util.delete(Node.getRoot(request));
		}
	}


	public void testException()
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(unitNamespace,"exceptionTest");
			Node.createTextElement("intVal","100",request);
			Node.createTextElement("intVal","0",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
			if (!result.equals("Method.Invocation Error"))
			{
				//fail("testException Expected value is Method.Invocation Error, but returned " +result);
			}
			//String refVal = Find.firstDataWithDefault(response,"?<defaultByRefArgs><intVal>","False");
            String refVal = Node.getDataWithDefault(
                    new XPath(".//defaultByRefArgs/intVal").firstMatch(response, null),
                    "False");
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : voidStaticRefMixedArgs :: "+ex.toString() );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
		}
	}


}
